<?php

class ButtonsController extends AppController {
    
    public $layout = "admin";

    public $sl_botones = array(
        "actions" => array("V" => "view", "E" => "edit", "D" => "delete"),
        "filters" => array(
            array("name" => 'Button.description', "label" => 'Descripción')
        ),
        "columns" => array(
            array("name" => 'Button.description', "label" => 'Descripción')
        )
    );
    public $maint_botones = array(
        "fields" => array(
            "Descripción del Botón" =>
            array(
				array("name" => "fecha_carga", "label" => "Fecha Carga", "presentation" => "DATETIME", 'classparams' => '{"force":true}', 'isvisible' => false, "actions" => "A"),
                array("name" => 'description', "label" => 'Descripción'),
                array("name" => 'status', "label" => 'Estado', "presentation" => 'ESTADO'),
                array("name" => 'url', "label" => 'URL Destino'),
                array("name" => 'icon', "label" => 'Ícono Imagen'),
                array("name" => 'display_order', "label" => 'Orden'),
                array("name" => 'refresh', "label" => 'Refresco', "presentation" => 'SINO'),
                array("name" => 'counter_box', "label" => 'ID Box Contador'),
                array("name" => 'query', "label" => 'Query SQL', "presentation" => 'TEXTAREA', 'note' => 'Variables disponibles: #USER_ID#, #ROL_ID#')
            )
        )
    );

    public function beforeFilter() {
        $this->Auth->allow('ajax_buttons');
        parent::beforeFilter();
    }

    public function add() {
        $this->maint = $this->maint_botones;
        parent::add();
    }

    public function edit($id = null) {
        $this->maint = $this->maint_botones;
        parent::edit($id);
    }

    public function index() {
        $this->search_list = $this->sl_botones;
        parent::index();
    }

    public function view($id = null) {
        $this->maint = $this->maint_botones;
        parent::view($id);
    }

    public function ajax_buttons() {
        $this->layout = "ajax";
        $model = $this->getModelName();

        $sql = "SELECT buttons.description, buttons.icon, buttons.counter_box, 
                       buttons.refresh, buttons.url, buttons.query 
                FROM buttons
                LEFT JOIN buttons_rols ON buttons_rols.button_id=buttons.id
                WHERE buttons.status='Activo' AND buttons_rols.rol_id=".$this->Auth->user("rol_id")."
                ORDER BY buttons.display_order ASC";
        $botones = $this->$model->Query($sql);

        foreach ($botones as $key => $boton) {
            if ($boton['buttons']['refresh'] == "Si" && !empty($boton['buttons']['query'])) {
                $search = array('#USER_ID#', '#ROL_ID#');
                $replace = array($this->Auth->user('id'), $this->Auth->user('rol_id'));
                $query = str_replace($search, $replace, $boton['buttons']['query']);
                
                $cant = $this->$model->Query($query);
                $botones[$key]['buttons']['cant'] = $cant[0][0]['cant'];
            }
            else {
                $botones[$key]['buttons']['cant'] = 0;
            }
            unset($botones[$key]['buttons']['query']);
        }

        $this->set('data', json_encode($botones));
    }

}